home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / other / checkmail.c next >
Encoding:
C/C++ Source or Header  |  1989-05-11  |  3.9 KB  |  190 lines

  1. /*
  2.  *            CHECKMAIL
  3.  *
  4.  *  Checks mail queues for messages from invoker (or all for mail su's)
  5.  *
  6.  *  Oct 84    H. Walter        initial version
  7.  *  Nov 85    C. Partridge        fix arginit() to not dereference 0!
  8.  *  Dec 85    D. Kingston        swallowed functions of checkmsgs
  9.  */
  10.  
  11. #include "util.h"
  12. #include "mmdf.h"
  13. #include <sys/stat.h>
  14. #include <pwd.h>
  15. #include "ch.h"
  16. #include "msg.h"
  17. #include "adr_queue.h"
  18.  
  19. extern    char    *quedfldir,        /* home directory for mmdf    */
  20.         *aquedir,        /* subordinate address directory */
  21.         *mquedir;        /* subordinate msg directory    */
  22.  
  23. DIR    *ovr_dirp;            /* handle on the queue directory */
  24. char    msg_sender[ADDRSIZE + 1];    /* return address of current message */
  25. int    realuid;            /* Invoker uid */
  26. int    alladdrs = 0;
  27. int    allmsgs = 0;
  28. int    fast = 0;
  29. char    bufout[BUFSIZ];
  30.  
  31. extern    int    errno;            /* system error number */
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     setbuf(stdout, bufout);
  38.     siginit();            /* standard interrupt initialization */
  39.  
  40.     /* distinguish different delivers */
  41.     arginit(argc, argv);
  42.  
  43.     /* get the real uid */
  44.     realuid = getuid();
  45.  
  46.     if (allmsgs)
  47.         setuid(realuid); /* Let unix protection do the hard work */
  48.  
  49.     mmdf_init(argv[0]);
  50.     mn_dirinit();        /* get right working directory */
  51.  
  52.     ovr_queue();        /* do the entire mail queue */
  53.  
  54.     exit(RP_OK);        /* "normal" end, even if pgm_bakgrnd */
  55. }
  56.  
  57. LOCFUN
  58. arginit(argc, argv)
  59. int    argc;
  60. char    **argv;
  61. {
  62.     register    int    ch;
  63.  
  64.     while ((ch = getopt(argc, argv, "cfm")) != EOF) {
  65.         switch (ch) {
  66.         case 'a':
  67.             alladdrs++;
  68.             break;
  69.  
  70.         case 'f':
  71.             fast++;        /* Don't get subject line */
  72.             break;
  73.  
  74.         case 'm':
  75.             allmsgs++;
  76.             break;
  77.  
  78.         default:
  79.             fprintf(stderr, "Usage: checkmail [-afm]\n");
  80.             exit(1);
  81.         }
  82.     }
  83. }
  84.  
  85. LOCFUN
  86. mn_dirinit()        /* get to the working directory */
  87. {
  88.     if (chdir(quedfldir) < OK) {
  89.         printf("can't chdir to %s\n", quedfldir);
  90.         exit(-1);
  91.     }
  92. }
  93.  
  94. ovr_queue()            /* Process entire message queue */
  95. {
  96.     struct    direct    *dp;
  97.  
  98.     if ((ovr_dirp = opendir(aquedir)) == NULL) {
  99.         printf("can't open address queue\n");
  100.         exit(-1);
  101.     }
  102.  
  103.     while ((dp = readdir(ovr_dirp)) != NULL) {
  104.         /* straight linear processing */
  105.         if (equal("msg.", dp->d_name, 4)) {
  106.             msg_proc(dp->d_name);
  107.         }
  108.     }
  109.  
  110.     closedir(ovr_dirp);
  111. }
  112.  
  113. /* msg_cite() is defined in adr_queue.h */
  114.  
  115. msg_proc(thename)        /* get, process & release a msg */
  116. char thename[];
  117. {
  118.     FILE        *msg_tfp;
  119.     Msg        themsg;
  120.     struct    stat    sb;
  121.     char        msgname[LINESIZE];
  122.     char        linebuf[LINESIZE];
  123.  
  124.     strcpy(themsg.mg_mname, thename);
  125.     sprintf(msgname, "%s%s", mquedir, thename);
  126.  
  127.     if (stat(msgname, &sb) < 0) {
  128.         perror(msgname);
  129.         return;
  130.     }
  131.     /* Invoker's file? */
  132.     if (allmsgs || sb.st_uid == realuid)
  133.         if (mq_rinit((Chan *) 0, &themsg, msg_sender) == OK) {
  134.             printf("Message %s: posted %.24s\n",
  135.                 thename, ctime(&themsg.mg_time));
  136.             if (allmsgs)
  137.                 printf("Return address: %s\n", msg_sender);
  138.             if (!fast) {
  139.                 if ((msg_tfp = fopen(msgname, "r")) == NULL)
  140.                     printf("can't open msg file '%s'\n",msgname);
  141.                 else {
  142.                     while (fgets(linebuf, sizeof(linebuf), msg_tfp) != NULL) {
  143.                         if (prefix("subject:", linebuf) == TRUE) {
  144.                             printf("%s", linebuf);
  145.                             break;
  146.                         }
  147.                     }
  148.                     fclose(msg_tfp);
  149.                 }
  150.             }
  151.             adr_each();
  152.             mq_rkill(OK);
  153.         }
  154.         else {
  155.             printf("Message %s:  busy\n", thename);
  156.         }
  157. }
  158.  
  159. adr_each()            /* do each address */
  160. {
  161.     struct    adr_struct    theadr;
  162.  
  163.     for (;;) {
  164.         switch (mq_radr(&theadr)) {
  165.         case NOTOK:    /* rest of list must be skipped */
  166.         case DONE:    /* end of list */
  167.         default:
  168.             printf("\n");
  169.             return;
  170.  
  171.         case OK:
  172.             if (theadr.adr_delv != ADR_DONE) {
  173.                 if (theadr.adr_host && theadr.adr_host[0] !=(char) '\0')
  174.                     printf("   %s(via %s): not yet sent\n",
  175.                         theadr.adr_local, theadr.adr_host);
  176.                 else
  177.                     printf("   %s: not yet sent\n", theadr.adr_local);
  178.                 fflush(stdout);
  179.             } else if (alladdrs) {
  180.                 if (theadr.adr_host && theadr.adr_host[0] !=(char) '\0')
  181.                     printf("   %s(via %s): sent\n",
  182.                         theadr.adr_local, theadr.adr_host);
  183.                 else
  184.                     printf("   %s: sent\n", theadr.adr_local);
  185.                 fflush(stdout);
  186.             }
  187.         }
  188.     }
  189. }
  190.